package com.mediacloud.app.newsmodule.fragment.works

import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.facebook.drawee.view.SimpleDraweeView
import com.mediacloud.app.assembly.util.loadByFresco
import com.mediacloud.app.newsmodule.R

class SubmitLibraryAdapter : BaseQuickAdapter<ArticleBean,BaseViewHolder>(R.layout.item_article_works){
    override fun convert(helper: BaseViewHolder, item: ArticleBean?) {
        item?.let{
            val logo = helper.getView<SimpleDraweeView>(R.id.logoView)
            val ivVideo = helper.getView<View>(R.id.ivVideo)
            val titleTv = helper.getView<TextView>(R.id.titleTv)
            val timeTv = helper.getView<TextView>(R.id.timeTv)
            val lineView = helper.getView<View>(R.id.lineView)

            timeTv.text = item.addTime
            titleTv.text = item.title
            if (TextUtils.isEmpty(item.logo)){
                setLayout(false,titleTv,timeTv,lineView)
                logo.visibility = View.GONE
            }else{
                setLayout(true,titleTv,timeTv,lineView)
                logo.visibility = View.VISIBLE
                logo.loadByFresco(item.logo)
                if (item.type == "5"){
                    ivVideo.visibility = View.VISIBLE
                }else{
                    ivVideo.visibility = View.GONE
                }
            }
            if (item.pickStatus == "1")
                timeTv.setCompoundDrawablesWithIntrinsicBounds(ContextCompat.getDrawable(timeTv.context,R.drawable.icon_picked),null,null,null)
            else
                timeTv.setCompoundDrawablesWithIntrinsicBounds(null,null,null,null)
        }


    }

    fun setLayout(
        hasLogo: Boolean,
        text_title: View,
        text_time: View,
        lineView: View,
    ) {
        val titleLp = text_title.layoutParams as ConstraintLayout.LayoutParams
        val timeLp = text_time.layoutParams as ConstraintLayout.LayoutParams
        val lineLp = lineView.layoutParams as ConstraintLayout.LayoutParams

        if (hasLogo) {

            titleLp.endToStart = R.id.logoView
            titleLp.endToEnd = ConstraintLayout.LayoutParams.UNSET
            titleLp.marginEnd = text_title.context.resources.getDimensionPixelSize(R.dimen.dimen16)

            timeLp.topToBottom = ConstraintLayout.LayoutParams.UNSET
            timeLp.bottomToBottom = R.id.logoView
            timeLp.topMargin = 0

            lineLp.topToBottom = R.id.logoView


        } else {
            titleLp.endToStart = ConstraintLayout.LayoutParams.UNSET
            titleLp.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID
            titleLp.marginEnd = 0

            timeLp.topToBottom = R.id.titleTv
            timeLp.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
            timeLp.topMargin = text_time.context.resources.getDimensionPixelSize(R.dimen.dimen12)

            lineLp.topToBottom = R.id.timeTv
        }
        text_title.layoutParams = titleLp
        text_time.layoutParams = timeLp
        lineView.layoutParams = lineLp

    }
}